package com.ruoyi.web.controller.diagmanage;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.diagmanage.domain.DatabaseReport;
import com.ruoyi.diagmanage.domain.Intelligent;
import com.ruoyi.diagmanage.domain.MainReport;
import com.ruoyi.diagmanage.service.MainReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/MainReportController")
public class MainReportController {

    @Autowired
    private MainReportService mainReportService;

    public static String interceptTime(String timeStr) {
        if (!StringUtils.isBlank(timeStr)) {
            if (timeStr.contains("T")) {
                DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
                LocalDateTime ldt = LocalDateTime.parse(timeStr, df);
                ZoneId currentZone = ZoneId.of("UTC");
                ZoneId newZone = ZoneId.of("Asia/Shanghai");
                timeStr = ldt.atZone(currentZone).withZoneSameInstant(newZone).toLocalDateTime().toString();
            }
            if (timeStr.length() >= 10) {
                return timeStr.substring(0, 10);
            }
        }
        return timeStr;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }

    @Log(title = "巡检报告查询", businessType = BusinessType.QUERY)
    @PostMapping("/queryMainReport")
    public AjaxResult queryMainReportList(@RequestBody MainReport mainReport)
    {
        mainReport.setBeginTime(interceptTime(mainReport.getBeginTime()));
        mainReport.setCreateTime(interceptTime(mainReport.getCreateTime()));
        mainReport.setFinishTime(interceptTime(mainReport.getFinishTime()));
        List<MainReport> mainReports = mainReportService.queryMainReportList(mainReport);
        return success(mainReports);
    }

    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MainReport mainReport)
    {
        List<MainReport> mainReports = mainReportService.queryMainReportList(mainReport);

        ExcelUtil<MainReport> util = new ExcelUtil<MainReport>(MainReport.class);

        util.exportExcel(response, mainReports, "数据库分析结果");
    }




}
